package com.prs.crm.service.fs.impl;

import java.io.File;
import java.util.Collection;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.fs.SalarySettleDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.fs.Detail;
import com.prs.crm.domain.fs.SalarySettle;
import com.prs.crm.domain.hr.PayrollBill;
import com.prs.crm.service.fs.DetailService;
import com.prs.crm.service.fs.SalarySettleService;
import com.prs.crm.service.hr.PayrollService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class SalarySettleServiceImpl extends BaseServiceImpl<SalarySettle,SalarySettleDao> implements SalarySettleService{
	private DetailService detailService;
	private PayrollService payrollService;
	
	@Override
	public void submitSalarySettle(SalarySettle salarySettle, File file,
			String fileName) {
		// TODO Auto-generated method stub
		Attachment attach = FileUtil.getInstance().createAttach(file, fileName);
		if(attach != null) {
			salarySettle.setAttach(attach);
		}
		Double increaseValue = 0.0;
		Double cutValue = 0.0;
		Collection<PayrollBill> payrollBills = salarySettle.getPayroll().getPayrollBills();
		for(PayrollBill payrollBill : payrollBills) {
			if(payrollBill.getType().equals("增项")) {
				increaseValue += payrollBill.getValue();
			}else if(payrollBill.getType().equals("减项")) {
				cutValue += payrollBill.getValue();
			}
		}
		salarySettle.setMoney(increaseValue - cutValue);
		salarySettle.setState(Constrains.TASK_AUDITING);
		this.getDao().save(salarySettle);
		
		salarySettle.getPayroll().setState(Constrains.PAYROLL_FINISH);
		this.getPayrollService().save(salarySettle.getPayroll());
		
		Double balance = this.getDetailService().getBalanceByAccount(salarySettle.getBankAccount());		
		Detail detail = new Detail();
		detail.setBankAccount(salarySettle.getBankAccount());
		detail.setItem(Constrains.FS_ITEMS_SALARYSETTLE);
		detail.setPay(salarySettle.getMoney());
		detail.setIncome(0.00);
		detail.setSalarySettle(salarySettle);
		detail.setBalance(balance - salarySettle.getMoney());
		this.getDetailService().save(detail);
	}

	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}

	public DetailService getDetailService() {
		return detailService;
	}

	public void setPayrollService(PayrollService payrollService) {
		this.payrollService = payrollService;
	}

	public PayrollService getPayrollService() {
		return payrollService;
	}

}
